import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yp/pages/mall_app/account_safe/logic.dart';
import 'package:yp/pages/mall_app/account_safe/state.dart';
import 'package:yp/router.dart';

class AccountSafePage extends StatelessWidget {
  final AccountSafeController logic = Get.put(AccountSafeController());
  final AccountSafeState state = Get.find<AccountSafeController>().state;
  @override
  Widget build(BuildContext context) {
    return FlutterEasyLoading(
        child: Scaffold(
            appBar: AppBar(
              backgroundColor: Config.mainColor,
              title: Text(
                "账号安全",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 34.sp,
                ),
              ),
              centerTitle: true,
              elevation: 0,
            ),
            body: GetBuilder<AccountSafeController>(
              builder: (context) => Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.sp),
                child: Column(
                  children: [
                    getItem(
                        name: '手机号',
                        content: state.photo,
                        contentColor: Color(0xff222222),
                        callback: () {
                          Get.toNamed(RouteConfig.verifyPhone);
                        }),
                    Divider(
                      height: 1,
                      color: Color(0xffeeeeee),
                    ),
                    getItem(
                        name: '支付密码',
                        content: state.passwordFlag ? '已设置' : '设置',
                        contentColor: Config.mainColor,
                        callback: () async {
                          var result = await Get.toNamed(RouteConfig.setPayPwd);
                          if (result == true) {
                            state.passwordFlag = true;
                            logic.update();
                          }
                        }),
                    Divider(
                      height: 1,
                      color: Color(0xffeeeeee),
                    ),
                    SizedBox(
                      height: 100.sp,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text.rich(TextSpan(
                              text: '免密支付',
                              style: TextStyle(
                                  color: Color(0xff222222), fontSize: 32.sp),
                              children: [
                                TextSpan(
                                    text: '(单笔消费不满200元无需密码)',
                                    style: TextStyle(
                                        color: Color(0xffcdcdcd),
                                        fontSize: 24.sp))
                              ])),
                          CupertinoSwitch(
                              value: state.freeOpenSign,
                              onChanged: (_) {
                                logic.setNoPwdPay();
                              })
                        ],
                      ),
                    ),
                    Divider(
                      height: 1,
                      color: Color(0xffeeeeee),
                    ),
                  ],
                ),
              ),
            )));
  }

  Widget getItem(
      {String name,
      String content,
      Color contentColor,
      VoidCallback callback}) {
    return InkWell(
      onTap: callback,
      child: SizedBox(
        height: 100.sp,
        child: Row(
          children: [
            Expanded(
                child: Text(
              name,
              style: TextStyle(color: Color(0xff222222), fontSize: 32.sp),
            )),
            Text(
              content,
              style: TextStyle(color: contentColor, fontSize: 32.sp),
            ),
            Icon(
              Icons.arrow_forward_ios,
              size: 30.sp,
              color: Color(0xff999999),
            )
          ],
        ),
      ),
    );
  }
}
